package cn.bob.org.service.impl;

import cn.bob.basic.query.BaseQuery;
import cn.bob.basic.service.IBaseService;
import cn.bob.basic.service.impl.BaseServiceImpl;
import cn.bob.basic.utils.MD5Utils;
import cn.bob.basic.utils.PageList;
import cn.bob.basic.utils.StrUtils;
import cn.bob.org.domain.Department;
import cn.bob.org.domain.Employee;
import cn.bob.org.mapper.EmployeeMapper;
import cn.bob.org.service.IEmployeeService;
import cn.bob.user.domain.Logininfo;
import cn.bob.user.domain.User;
import cn.bob.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author bob
 * @data 2022/9/28-10:55
 */
@Service
public class EmployeeServiceImpl  extends BaseServiceImpl<Employee> implements IEmployeeService {
    @Autowired
    private EmployeeMapper mapper;

    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    public List<Employee> deawer(Long id) {
        return mapper.deawer(id);
    }

    @Override
    public List<Employee> selectByShopId(Long shopId) {
        return mapper.findByShopId(shopId);
    }

    @Override
    public void add(Employee employee) {
        //获取盐值
        String salt = StrUtils.getComplexRandomString(32);
        //获取base64密码
        String password = MD5Utils.encrypByMd5(salt + employee.getPassword());
        employee.setSalt(salt);
        employee.setPassword(password);

        //将emp存储为logininfo  创建的系统用户
        Logininfo logininfo = empConventLog(employee);
        logininfo.setType(0);
        logininfoMapper.add(logininfo);

        employee.setLogininfoId(logininfo.getId());
        mapper.add(employee);
        //添加员工角色中间表数据
        mapper.addEmpMidden(employee.getId(),employee.getRoles());

    }

    //将employee转换为logininfo
    public Logininfo empConventLog(Employee employee){
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee,logininfo);
        System.out.println(logininfo);
        return logininfo;
    }

    //修改emp 和 中间表
    @Override
    public void update(Employee employee) {
        //获取盐值
        String salt = employee.getSalt();
        //获取base64密码
        String password = MD5Utils.encrypByMd5(salt + employee.getPassword());
        employee.setPassword(password);

        //修改对应id的logininfo的数据
        Logininfo logininfo = empConventLog(employee);
        logininfo.setId(employee.getLogininfoId());
        logininfoMapper.update(logininfo);

        mapper.update(employee);
        //先删除后添加
        mapper.delEmpMidden(employee.getId());
        mapper.addEmpMidden(employee.getId(),employee.getRoles());
    }

    //删除中间表
    @Override
    public void delById(Long id) {
        //查询到emp再获取loginifo的id进行删除
        Employee byId = mapper.findById(id);
        Long logininfoId = byId.getLogininfoId();
        super.delById(id);
        //删除logininfo的数据
        logininfoMapper.delById(logininfoId);
        mapper.delEmpMidden(id);
    }


    //批量删除中间表
    @Override
    public void batchDel(List<Long> ids) {
        //获取logininfo的id集合
        List<Employee> emps = ids.stream().map(id -> mapper.findById(id)).collect(Collectors.toList());
        List<Long> loginIds = emps.stream().map(Employee::getLogininfoId).collect(Collectors.toList());
        super.batchDel(ids);
        //删除
        logininfoMapper.batchDel(loginIds);
        mapper.batchDelEmpMidden(ids);
    }

    //分页查询
    //将role的下拉框数据返回给前端
    @Override
    public PageList<Employee> queryByPage(BaseQuery query) {
        PageList<Employee> employeePageList = super.queryByPage(query);
        List<Employee> emps = employeePageList.getRows();
        //设置值到roles集合中
        for (Employee emp : emps) {
            List<Long> roles= mapper.findEmpMidden(emp.getId());
            emp.setRoles(roles);
        }
        return new PageList<Employee>(emps,employeePageList.getTotal());
    }
}
